/*
**
** Description
** This file contains utility routines which are used by other programs.
**
*/

#include	<iostream>
#include	<fstream>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<time.h>

#include "typedef.h"
#include "msgbus_lib.h"
#include "hdb_lib.h"

#ifdef	_DUPLICAT
#include "msgbus_lib.h"
#include "duplicat_sybase_lib/duplicat_sybase_define.h"
#include "duplicat_sybase_lib/duplicat_sybase_struct.h"
extern class   duplicat_info modify_table_info; //98.10.09
#endif

extern class IHdbConnection *m_HdbConn;

#include	"public.h"
#include	"pubutils.h"
#include	"load.h"


#if !lint
static char Sccsid[] = {"%Z% %M% %I% %G%"};
#endif /* !lint */

#include	"../inc/FileMgr.h"



void
pub_error(char *msg)
{
   fprintf(PUB_ERROR_OUT, "ERROR: %s \n", msg);
   fflush(PUB_ERROR_OUT);
}


int
pub_execute_cmd(char *sql_str, short *error_no, char *error_message)
{
	int		retcode;

	retcode = m_HdbConn->execute_cmd_commit(sql_str, error_no, error_message);
	if (retcode == -1)
		return	-1;

#ifdef	_DUPLICAT 
      	modify_table_info.save_one_report(DUPLICAT_SEND, //98.10.09
                DUPLICAT_SQL_SETTENCE,
                strlen(sql_str)+1,
                sql_str);
#endif

	*error_no = 0;

	return	0;
	
}


int pub_execute_cmd_without_commit(char *sql_str, short *error_no, char *error_message)
{
	int		retcode;

	retcode = m_HdbConn->execute_cmd_without_commit(sql_str, error_no, error_message);
	if (retcode == -1)
		return	-1;

	*error_no = 0;

	return	0;
	
}

#ifdef	_DUPLICAT
void send_sql_to_duplicat(char *sql_str)
{	
      	modify_table_info.save_one_report(DUPLICAT_SEND, //98.10.09
                DUPLICAT_SQL_SETTENCE,
                strlen(sql_str)+1,
                sql_str);
}
#endif

char	*
translate_time_to_ora_string(int seconds)
{
	char		*time_str;
	time_str = (char *) malloc(100);

	CHdbCommon hdbCommon;
	hdbCommon.time_t_to_date_string(seconds, time_str);
	
	return time_str;	
}

/***********************************************************************
** This function converts the keyid with type char(13) to keyid_type
** type KEYID_TYPE
***********************************************************************/

KEYID_TYPE * 
pub_keyid_to_struct(char *keyid)
{
        KEYID_TYPE      *keyid_type;
        char            record_str[11], column_str[4];
        int             tmp_int;
        
        keyid_type = (KEYID_TYPE *) malloc(sizeof(KEYID_TYPE));
        memcpy(record_str, keyid, 10);
        record_str[10] = '\0';
        memcpy(column_str, keyid + 10, 3);
        column_str[3] = '\0';

        sscanf(record_str, "%d", &tmp_int);
        keyid_type->record_id = tmp_int;
        sscanf(column_str, "%d", &tmp_int);
        keyid_type->column_id = tmp_int;

        return keyid_type;

}


/***********************************************************************
** This function converts the keyid with type KEYID_TYPE to keyid_type
** type char(13)
***********************************************************************/
char    *
pub_struct_to_keyid(KEYID_TYPE  key_struct)
{
        char            *keyid, record_str[11], column_str[4];
        int             i, length_record, length_column;

        keyid = (char *) malloc(14);
        sprintf(record_str, "%d", key_struct.record_id);
        length_record = 10 - strlen(record_str);
        for (i = 0; i < length_record; i++)
                keyid[i] = '0';
        memcpy(keyid + length_record, record_str, strlen(record_str));

        sprintf(column_str, "%d", key_struct.column_id);
        length_column = 3 - strlen(column_str);
        for (i = 0; i < length_column; i++)
                keyid[length_record + strlen(record_str) + i] = '0';
        memcpy(keyid + length_record + strlen(record_str) + length_column, column_str, strlen(column_str));

        keyid[13] = '\0';

        return keyid;

}


/***********************************************************************
** This function converts the binary to the string biggin with "0x"
***********************************************************************/
char	*
pub_binary_to_string(char *bin_mem, int bin_length)
{
	int		i;
	unsigned char	tmp_char;
	char		*return_str, convert_str[10];
	char		*pointer;

	if (bin_length <= 0)
		return	NULL;

	return_str = (char *) malloc(600);
	pointer = bin_mem;
	sprintf(return_str, "");
	for (i = 0; i < bin_length; i++)
	{
		memcpy(&tmp_char, pointer, sizeof(unsigned char));
		pointer += sizeof(unsigned char);
		sprintf(convert_str, "%02x", tmp_char);
		strcat(return_str, convert_str);
	}
	strcat(return_str, "");

	return	return_str;

}


// judge if occur_time exists in the specified sample table
int
exists_occur_time(char *table_name, char *time_str)
{
	char		sql_statement[1000];
	char		*result_ptr;
	RESULT_HEAD2	*result_head;
	FIELD_INFO	*field_info;
	int		return_val;
	int		retcode;

	sprintf(sql_statement, "select 1 from %s where occur_time = %s", table_name, time_str);
	retcode = get_query_result(sql_statement, NULL_IS_ZERO, &result_ptr);
	if (retcode <0)
	{
		pub_error("get exists_occur_time error!");
		return -1;
	}
	else
	{
		get_query_head(result_ptr, &result_head, &field_info);
		if (result_head->data_num == 0)
			return_val = 0;
		else
			return_val = 1;
	}

	free(result_ptr);
	if (result_head)
		free(result_head);
	if (field_info)
		free(field_info);

	return return_val;

}

// get the large report buffer
int
get_large_report_buffer(char *old_buffer, unsigned int old_length, char end_flag, unsigned int send_length, char *send_buffer, unsigned int *new_length, char **new_buffer)
{
        char    *buffer;
        
        if (old_buffer == NULL)
        {
                buffer = (char *) malloc(send_length);
                memcpy(buffer, send_buffer, send_length);
                *new_buffer = buffer;
                *new_length = send_length;
        }
        else
        {
                buffer = (char *) malloc(old_length + send_length);
                memcpy(buffer, old_buffer, old_length);
                memcpy(buffer + old_length, send_buffer, send_length);
                *new_buffer = buffer;
                *new_length = old_length + send_length;
                free(old_buffer);
        }
        free(send_buffer);
        
        if (end_flag == 0)
                return 0;
        else
                return 1;
        
}


// Added by ZJH in SZ 00.09.28
// get statistic_mode stored in ../sys/statistic_mode.sys for diffrent statistic mode
unsigned char	get_statistic_mode()
{
	FILE		*fp;
	char		tmp_str[100], mark[100], mark1[100];
	unsigned char	stat_mode;
	int		ret;

	FileMgr filemgr;
	int	line = 0, count = 0, isok = -1;
	char    **p;

	filemgr.ReadFile("statistic_mode.sys", line, p );

	stat_mode = STAT_MODE_DEF_VALUE;

	if( line <= 0 ) return stat_mode;
	
	count = 0;
	while(1)
	{
		if( count >= line ) break;
		if( filemgr.IsLineValid(p[count]) >= 0 ){
			ret = sscanf(p[count],"%s %s %s ",mark, mark1, tmp_str);
//			printf("mark=%s, mark1=%s, tmp_str=%s\n", mark, mark1, tmp_str);
			if( ret < 0 ) break;
			if (strcasecmp(tmp_str, "ABS") == 0)
				stat_mode = STAT_MODE_DEF_ABS;
			else
				stat_mode = STAT_MODE_DEF_VALUE;
		}
		count++;
	}

	for( int i = 0; i < line; i ++ )	delete[] p[i];
	delete[] p;

	return stat_mode;
}

// End of Added
		
